$(document).ready(function () {
	// Dialog
	$('#dialog').dialog({
		autoOpen: false,
		modal: true,
		closeOnEscape: false,
		width: 580,
		top: 20,
		draggable: false,
		resizable: false,
		dialogClass: "popupGroupOn"
	});
	
	$("#dialog .login, #dialog .register").css("display", "none");
	
	$("#dialog .register input[name=birthday]").datepicker({ 
		dateFormat: "yy-mm-dd", 
		changeMonth: true, 
		changeYear: true, 
		yearRange: "1900:2012",
		monthNamesShort:  ["Tháng 1", "Tháng 2", "Tháng 3", "Tháng 4", "Tháng 5", "Tháng 6", "Tháng 7", "Tháng 8", "Tháng 9", "Tháng 10", "Tháng 11", "Tháng 12"],
		dayNamesMin: ["CN", "T2", "T3", "T4", "T5", "T6", "T7"]
	});
	
	//Login button event
	$(".topLogin .userLogin a").click(function () {
		$("#dialog .login").css("display", "block");
		$("#dialog .register").css("display", "none");
		$('#dialog').dialog("open");
		return false;
	});
	
	/*//Register button event
	$(".topLogin .userRegister a").click(function () {
		$("#dialog .login").css("display", "none");
		$("#dialog .register").css("display", "block");
		$('#dialog').dialog("open");
		return false;
	});*/
	
	$("#dialog  .close").click(function () {
		$("#dialog .login").css("display", "none");
		$("#dialog .register").css("display", "none");
		$('#dialog').dialog("close");
	});
	
	//Check valid login
	$("#dialog .login input[name=email], .loginPopup input[name=password]").focus(function () {
		$(this).parent().removeClass("error");
		$(this).parent().find(".triangleUp").fadeOut(100);
		$(this).parent().find(".errorMessage").fadeOut(100);
	});
	
	$("#dialog .login form").bind("submit", function (event) {
		var email = $("#dialog .login input[name=email]");
		var password = $("#dialog .login input[name=password]");
		
		var pattern = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
		if (email.val() == "" || !pattern.test(email.val())) {
			email.parent().addClass("error");
			email.parent().find(".triangleUp").fadeIn(100);
			email.parent().find(".errorMessage").fadeIn(100);
		}
		
		if (password.val() == "") {
			password.parent().addClass("error");
			password.parent().find(".triangleUp").fadeIn(100);
			password.parent().find(".errorMessage").fadeIn(100);
		}
		
		//check username
		if ($("#dialog .login label.error").length == 0) {
			var form_data = {email: email.val(), password: password.val()};
			$.ajax({
				type: "GET",
				url: "/ajax-login",
				data: form_data,
				dataType: "json",
				success: function(response)
				{
					//check is success
					if (response.success == false) {
						if (response.email) {
							email.parent().addClass("error");
							email.parent().find(".triangleUp").fadeIn(100);
							email.parent().find(".errorMessage").html(response.email).fadeIn(100);
						}
						if (response.password) {
							password.parent().addClass("error");
							password.parent().find(".triangleUp").fadeIn(100);
							password.parent().find(".errorMessage").html(response.password).fadeIn(100);
						}
					} else {
						location.reload();
					}
				}
			});
		}
		
		return false;
	});
	
	//Check valid register
	$("#dialog .register input, #dialog .register select").focus(function () {
		$(this).parents("label").removeClass("error");
		$(this).parents("div").removeClass("error");
	});
	
	$("#dialog .register form").bind("submit", function (event) {
		var email = $("#dialog .register input[name=email]");
		var password = $("#dialog .register input[name=password]");
		var repassword = $("#dialog .register input[name=repassword]");
		var name = $("#dialog .register input[name=name]");
		var gender = $("#dialog .register input[name=gender]");
		var phone = $("#dialog .register input[name=phone]");
		var birthday = $("#dialog .register input[name=birthday]");
		var pattern = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
		if (email.val() == "" || !pattern.test(email.val())) {
			email.parent().addClass("error");
		}
		
		if (password.val() == "") {
			password.parent().addClass("error");
		} else if (repassword.val() == "") {
			repassword.parent().addClass("error");
		} else if (password.val() != repassword.val()) {
			repassword.parent().addClass("error");
			repassword.parent().find(".errorMessage").html("Mật khẩu và xác nhận mật khẩu không giống nhau. Xin vui lòng nhập lại.");
		}
		
		if (name.val() == "") {
			name.parent().addClass("error");
		}
		
		if (phone.val() == "") {
			phone.parent().addClass("error");
		}
		
		if ($("#dialog .register .error").length == 0) {
			var form_data = {
					username: email.val(), 
					password: password.val(),
					repassword: repassword.val(),
					name: name.val(),
					gender: gender.val(),
					phone: phone.val(),
					birthday: birthday.val()
				};
			$.ajax({
				type: "GET",
				url: "/ajax-register",
				data: form_data,
				dataType: "json",
				success: function(response)
				{
					//check is success
					if (response.success == false) {
						if (response.username) {
							email.parent().addClass("error");
							email.parent().find(".errorMessage").html(response.username);
						}
						
						if (response.password) {
							password.parent().addClass("error");
							password.parent().find(".errorMessage").html(response.password);
						}
						
						if (response.repassword) {
							repassword.parent().addClass("error");
							repassword.parent().find(".errorMessage").html(response.repassword);
						}
						
						if (response.name) {
							name.parent().addClass("error");
							name.parent().find(".errorMessage").html(response.name);
						}
						
						if (response.gender) {
							gender.parent().addClass("error");
							gender.parent().find(".errorMessage").html(response.gender);
						}
						
						if (response.phone) {
							phone.parent().addClass("error");
							phone.parent().find(".errorMessage").html(response.phone);
						}
						
						if (response.birthday) {
							birthday.parent().addClass("error");
							birthday.parent().find(".errorMessage").html(response.birthday);
						}
						
					} else {
						location.reload();
					}
				}
			});
		}
		
		return false;
	});
	
	//forgot-password form
	$('#forgot-password').dialog({
		autoOpen: false,
		modal: true,
		closeOnEscape: false,
		width: 450,
		top: 20,
		draggable: false,
		resizable: false,
		dialogClass: "popupGroupOn"
	});
	
	$(".forgotPassword").click(function () {
		$('#dialog').dialog("close");
		$('#forgot-password').dialog("open");
	});
	
	$("#forgot-password .close").click(function () {
		$('#forgot-password').dialog("close");
	});
	
	var input_email = $("#forgot-password form input[name=email]");
	var forgot_email = input_email.val();
	
	input_email.focus(function () {
		var email = input_email.val();
		if (email == forgot_email) {
			$(this).val("");
		}
		input_email.parent().find(".error").html("");
	});
	
	input_email.focusout(function () {
		var email = input_email.val();
		if (email == "") {
			$(this).val(forgot_email);
		}
	});
	
	//Remove event enter keypress
	input_email.keypress(function(e){
		if ( e.which == 13 ) return false;
	});
	
	$("#forgot-password form a.continue")
	$("#forgot-password form a.continue").bind("click", forgotpass);
	
	function forgotpass() {
		var email = input_email.val();
		var pattern = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
		if (input_email.val() == "" || !pattern.test(input_email.val())) {
			input_email.parent().find(".error").html("Vui lòng nhập email hợp lệ.");
			return false;
		}
		$("#forgot-password form a.continue").unbind("click");
		$.ajax({
			type: "GET",
			url: "/ajax-forgot-password-email",
			data: {email: email},
			dataType: "json",
			success: function(response)
			{
				$('#forgot-password').dialog("close");
				alert("Thông tin hỗ trợ đã được gửi vào " + email + " \n Quý khách vui lòng làm theo hướng dẫn trong email");
				$("#forgot-password form a.continue").bind("click", forgotpass);
			}
		});
		
		return false;
	}
});